package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.utils.PageUtils;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.system.service.ISysDeptService;
import com.xjy.system.service.ISysPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjy.system.domain.bo.SysStaffAddBo;
import com.xjy.system.domain.bo.SysStaffQueryBo;
import com.xjy.system.domain.bo.SysStaffEditBo;
import com.xjy.system.domain.SysStaff;
import com.xjy.system.mapper.SysStaffMapper;
import com.xjy.system.domain.vo.SysStaffVo;
import com.xjy.system.service.ISysStaffService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 员工管理Service业务层处理
 *
 * @author xjy
 * @date 2022-02-17
 */
@Service
public class SysStaffServiceImpl extends ServicePlusImpl<SysStaffMapper, SysStaff> implements ISysStaffService {
	@Autowired
	private ISysDeptService deptService;
	@Autowired
	private ISysPostService postService;
    @Override
    public SysStaffVo queryById(Long staffId){
        return getVoById(staffId, SysStaffVo.class);
    }

    @Override
    public TableDataInfo<SysStaffVo> queryPageList(SysStaffQueryBo bo) {
        PagePlus<SysStaff, SysStaffVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysStaffVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysStaffVo> queryList(SysStaffQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysStaffVo.class);
    }

    private LambdaQueryWrapper<SysStaff> buildQueryWrapper(SysStaffQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysStaff> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, SysStaff::getDeptId, bo.getDeptId());
        lqw.like(StrUtil.isNotBlank(bo.getDeptName()), SysStaff::getDeptName, bo.getDeptName());
        lqw.eq(StrUtil.isNotBlank(bo.getJobNum()), SysStaff::getJobNum, bo.getJobNum());
        lqw.like(StrUtil.isNotBlank(bo.getStaffName()), SysStaff::getStaffName, bo.getStaffName());
        lqw.eq(StrUtil.isNotBlank(bo.getStaffSex()), SysStaff::getStaffSex, bo.getStaffSex());
        lqw.eq(bo.getDutyId() != null, SysStaff::getDutyId, bo.getDutyId());
        lqw.like(StrUtil.isNotBlank(bo.getDutyName()), SysStaff::getDutyName, bo.getDutyName());
        lqw.eq(StrUtil.isNotBlank(bo.getStaffPhone()), SysStaff::getStaffPhone, bo.getStaffPhone());
        lqw.eq(StrUtil.isNotBlank(bo.getStaffEmail()), SysStaff::getStaffEmail, bo.getStaffEmail());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysStaffAddBo bo) {
        SysStaff add = BeanUtil.toBean(bo, SysStaff.class);
        add.setDeptName(deptService.selectDeptById(bo.getDeptId()).getDeptName());
        add.setDutyName(postService.selectPostById(bo.getDutyId()).getPostName());
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysStaffEditBo bo) {
        SysStaff update = BeanUtil.toBean(bo, SysStaff.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysStaff entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

	@Override
	public List<SysStaff> querydeptById(Long deptId) {

    	return baseMapper.querydeptById(deptId);
	}
}
